import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-06-02
 * Time: 16:27
 */
public class groupAnagrams {
    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> hash = new HashMap<>();

        for(int i = 0; i < strs.length; i++) {
            char[] ch = strs[i].toCharArray();
            Arrays.sort(ch);
            String key = new String(ch);
            if(!hash.containsKey(key)) {
                hash.put(key,new ArrayList<>());
            }
            hash.get(key).add(strs[i]);
        }

        return new ArrayList<>(hash.values());
    }
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入字符串数组以逗号分割：");
            String input = scanner.nextLine();
            String[] strs = input.split(",");
            // 以空格分割 input.split("\\s+")
            List<List<String>> result = groupAnagrams(strs);
            // eat,tea,tan,ate,nat,bat
            System.out.println("分组结果：");
            System.out.println(result);
        }

}

